﻿using System;
using System.IO;

namespace Engine.DynamicObjectManager
{
    public class BaseObjectProperties
    {
        #region Private Fields

        /// <summary>
        /// Collision Shape:
        /// Self, Pylon, Bake, Heavybox, Lightbox
        /// </summary>
        private EnumBaseObjectCollisionModel collisionModel;

        /// <summary>
        /// the sound which will play when it is hit by car.
        /// </summary>
        private string collisionSound;

        /// <summary>
        /// the sound which played when hit the ground.
        /// </summary>
        private string fallSound;

        /// <summary>
        /// the objectname
        /// </summary>
        private string name;

        /// <summary>
        /// how should the object work with the engine together.
        /// </summary>
        private EnumBaseObjectType objectType;

        /// <summary>
        /// the weight of the object in the game.
        /// </summary>
        private int weight;

        #endregion Private Fields

        #region Public Constructors

        public BaseObjectProperties(string name)
        {
            this.name = name;
            this.collisionSound = "none";
            this.fallSound = "none";
            this.collisionModel = EnumBaseObjectCollisionModel.Self;
            this.objectType = EnumBaseObjectType.Static;
            this.weight = 0;
        }

        #endregion Public Constructors

        #region Public Properties

        public EnumBaseObjectType BaseObjectType
        {
            get { return this.objectType; }
            set { this.objectType = value; }
        }

        public EnumBaseObjectCollisionModel CollisionModel
        {
            get { return this.collisionModel; }
            set { this.collisionModel = value; }
        }

        public string CollisionSound
        {
            get { return this.collisionSound; }
            set { this.collisionSound = value; }
        }

        public string FallSound
        {
            get { return this.fallSound; }
            set { this.fallSound = value; }
        }

        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public int Weight
        {
            get { return this.weight; }
            set { this.weight = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public static BaseObjectProperties FromStream(BinaryReader br)
        {
            switch (br.ReadByte())
            {
                case 0x01:
                    return LoadV1(br);
            }
            throw new Exception("Unsupported Version!");
        }

        public void ToStream(BinaryWriter bw)
        {
            // Write version number
            bw.Write((byte)1);
            // write the properties
            bw.Write(this.name);
            bw.Write(this.collisionSound);
            bw.Write(this.fallSound);
            bw.Write((short)this.collisionModel);
            bw.Write((short)this.objectType);
            bw.Write(this.weight);
        }

        #endregion Public Methods

        #region Private Methods

        private static BaseObjectProperties LoadV1(BinaryReader br)
        {
            BaseObjectProperties objectPropertie = new BaseObjectProperties(br.ReadString());

            objectPropertie.collisionSound = br.ReadString();
            objectPropertie.fallSound = br.ReadString();
            objectPropertie.collisionModel = (EnumBaseObjectCollisionModel)br.ReadUInt16();
            objectPropertie.objectType = (EnumBaseObjectType)br.ReadUInt16();
            objectPropertie.weight = br.ReadInt32();

            return objectPropertie;
        }

        #endregion Private Methods
    }
}